package com.web.task;

import com.web.dao.MskPriceCountMapper;
import com.web.dao.PlatformMapper;
import com.web.entity.MskPriceCount;
import com.web.entity.Platform;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;

import static java.math.BigDecimal.ROUND_HALF_DOWN;

/**
 * @author FDjavaone
 * @date 2017/9/29
 */
@Component
@Configurable
@EnableScheduling
@Slf4j
public class MSKPriceRecordJob {
    @Autowired
    private PlatformMapper platformMapper;
    @Autowired
    private MskPriceCountMapper mskPriceCountMapper;

    @Scheduled(cron = "0 */1 * * * ?")
    public void MSKPriceRecord() throws Exception {
        Platform platform = platformMapper.selectByPrimaryKey(1L);
        Date date = new Date();
        MskPriceCount mskPriceCount = new MskPriceCount();
        mskPriceCount.setCreateTime(date);
        mskPriceCount.setModifyTime(date);
        if (platform.getMarketMsk().compareTo(new BigDecimal("0")) == 0) {
            mskPriceCount.setPrice(new BigDecimal(1));
        } else {
            mskPriceCount.setPrice(platform.getMarketFundPool().divide(platform.getMarketMsk(), 2, ROUND_HALF_DOWN));
        }

        mskPriceCountMapper.insertSelective(mskPriceCount);
    }
}
